package com.qingfeng.client.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qingfeng.base.exception.LsException;
import com.qingfeng.base.util.CodeEnum;
import com.qingfeng.client.VO.PostageQueryVo;
import com.qingfeng.client.VO.PostageVo;
import com.qingfeng.client.bean.PostageAddress;
import com.qingfeng.client.bean.PostageTemplate;
import com.qingfeng.client.dao.PostageAddressMapper;
import com.qingfeng.client.dao.PostageTemplateMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/4/29 2:09 下午
 */
@Service
public class PostageService {

    @Resource
    private PostageAddressMapper postageAddressMapper;

    @Resource
    private PostageTemplateMapper postageTemplateMapper;

    /**
     * 新增邮费模版
     * @param postageVo
     */
    @Transactional
    public void  save(PostageVo postageVo){
        List<PostageAddress> list = postageVo.getAddresses();
        PostageTemplate postageTemplate = postageVo;
        if (list==null){
            List defList = postageTemplateMapper.selectByStoreIdAndDef(postageTemplate.getStoreId(),true);
            if (defList.size()>0){
                throw new LsException(CodeEnum.ERROR.getCode(),"已有默认地址模版，请勿重复添加");
            }else {
                postageTemplate.setDef(true);
            }
        }

        List<PostageAddress> oldList = postageAddressMapper.selectByStoreId(postageTemplate.getStoreId());
        //得到两个数组的交集
        List<PostageAddress> sameList = list.stream().filter(item ->
                oldList.stream().anyMatch(
                        it->it.getCode()!=null
                                && item.getCode() != null
                                && it.getCode().equals(item.getCode()))
        ).collect(toList());
        if (sameList.size() != 0){
            throw new LsException(CodeEnum.ERROR.getCode(),"部分地区已经设置邮费模版");
        }

        postageTemplateMapper.insert(postageTemplate);

        if (list!=null && list.size()>0) {
            list.forEach(item -> {
                item.setStoreId(postageTemplate.getStoreId());
                item.setTemplateId(postageTemplate.getId());
            });
            postageAddressMapper.insertList(list);
        }
    }

    /**
     * 更新邮费模版
     * @param postageVo
     */
    @Transactional
    public void update(PostageVo postageVo){
        List<PostageAddress> list = postageVo.getAddresses();
        //去重
        list = list.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(
                                ()->new TreeSet<>(Comparator.comparing(PostageAddress::getCode))
                        ), ArrayList::new
                )
        );

        PostageTemplate postageTemplate = postageVo;
        postageTemplateMapper.updateById(postageTemplate);
        postageAddressMapper.deleteByTemplateId(postageTemplate.getId());
        if (list !=null && list.size()>0) {
            list.forEach(item -> {
                item.setStoreId(postageTemplate.getStoreId());
                item.setTemplateId(postageTemplate.getId());
            });
            postageAddressMapper.insertList(list);
        }
    }

    public IPage<PostageVo> query(PostageQueryVo postageQueryVo){
        return postageTemplateMapper.query(postageQueryVo.getPage(),postageQueryVo.getCondition());
    }

    /**
     * 删除模版信息，和对应的地址信息
     * @param templateIds
     * @param storeId
     */
    public void delete(List<Integer> templateIds,Integer storeId){
        postageTemplateMapper.deleteByIds(templateIds,storeId);
        postageAddressMapper.deleteByTemplateIds(templateIds,storeId);
    }

    public List<PostageAddress> getUsingAddress(Integer storeId){
       return postageAddressMapper.selectByStoreId(storeId);
    }
}
